package com.itlin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itlin.bean.Category;
import com.itlin.bean.PageParam;
import com.itlin.common.R;
import com.itlin.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 所谓伊人
 * @date 2022/3/24 - 9:27
 */
@Slf4j
@RequestMapping("/category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<Category> add(@RequestBody Category category) {
        int row = categoryService.add(category);

        if (row > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    @GetMapping("/page")
    public R<IPage<Category>> findPage(PageParam pageParam) {

        IPage<Category> page = categoryService.findPage(pageParam);

        return R.success(page);
    }

    @DeleteMapping
    public R<Category> delete(Long id, int type) {

        int row = categoryService.delete(id, type);

        if (row > 0) {
            return R.success("删除分类成功");
        }

        return R.error("删除分类失败");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改分类信息：{}",category);
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }

    @GetMapping("/list")
    public R<List<Category>> findByType(Integer type) {

        List<Category> categories = categoryService.fingByType(type);

        return R.success(categories);
    }
}
